1334E - Divisor Paths - CodeForces Solution


combinatorics graphs greedy math number theory *2200

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
const long long mod=998244353;
long long kq,ts[65],i,sl,nto[101],s,t,x,q,gt[102],ng[102];
long long lt(long long a,long long b)
{
    if(b==0) return 1;
    long long tg=lt(a,b/2);
    if(b%2==0) return tg*tg%mod;
    return tg*tg%mod*a%mod;
}
int main()
{
 //   freopen("ntu.inp","r",stdin);
 //   freopen("ntu.out","w",stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin>>x;
    for(i=2;i*i<=x;i++)
        if(x%i==0)
        {
            nto[++sl]=i;
            while(x%i==0) x/=i;
        }
    if(x>1) nto[++sl]=x;
    cin>>q;
    gt[0]=1;
    for(i=1;i<=101;i++) gt[i]=gt[i-1]*i%mod;
    ng[101]=lt(gt[101],mod-2);
    for(i=100;i>=0;i--) ng[i]=ng[i+1]*(i+1)%mod;
    while(q--)
    {
        cin>>s>>t;
        kq=1;
        x=__gcd(s,t);
        s/=x; t/=x;
        memset(ts,0,sizeof(ts));
        int dem=0;
        for(i=1;i<=sl;i++)
        {
            int cnt=0;
            while(s%nto[i]==0) { cnt++; s/=nto[i]; }
            dem+=cnt;
            kq=kq*ng[cnt]%mod;
        }
        kq=kq*gt[dem]%mod;
        dem=0;
        for(i=1;i<=sl;i++)
        {
            int cnt=0;
            while(t%nto[i]==0) { cnt++; t/=nto[i]; }
            dem+=cnt;
            kq=kq*ng[cnt]%mod;
        }
        kq=kq*gt[dem]%mod;
        cout<<kq<<'\n';
    }
}


Comments

Submit
0 Comments
More Questions

1372A - Omkar and Completion
159D - Palindrome pairs
981B - Businessmen Problems
1668A - Direction Change
1667B - Optimal Partition
1668B - Social Distance
88B - Keyboard
580B - Kefa and Company
960A - Check the string
1220A - Cards
897A - Scarborough Fair
1433B - Yet Another Bookshelf
1283B - Candies Division
1451B - Non-Substring Subsequence
1408B - Arrays Sum
1430A - Number of Apartments
1475A - Odd Divisor
1454B - Unique Bid Auction
978C - Letters
501B - Misha and Changing Handles
1496A - Split it
1666L - Labyrinth
1294B - Collecting Packages
1642B - Power Walking
1424M - Ancient Language
600C - Make Palindrome
1669D - Colorful Stamp
1669B - Triple
1669A - Division
1669H - Maximal AND